package com.cn.longdistancebusstation.scenic.order;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cn.longdistancebusstation.R;
import com.cn.longdistancebusstation.base.BaseActivity;
import com.cn.longdistancebusstation.httpHelper.HttpHelper;
import com.cn.longdistancebusstation.httpapi.HttpService;
import com.cn.longdistancebusstation.model.Contacter;
import com.cn.longdistancebusstation.model.ListResultV;
import com.cn.longdistancebusstation.model.Result;
import com.cn.longdistancebusstation.model.ScenicOrderDetails;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by mucheng on 2018/4/20.
 */

public class RefundTicketActivity extends BaseActivity implements View.OnClickListener{
    private TextView mStatusTextView;
    private TextView mTicketNameTextView;
    private TextView mTicketAmountTextView;
    private TextView mUseSituationTextView;
    private ImageView mOrderNoteImageView;
    private TextView mTicketIdTextView;
    private TextView mCreateTimeTextView;
    private RecyclerView mRecyclerView;
    private TouristInfoAdapter mTouristInfoAdapter;
    private String ticketId,status;
    private ScenicOrderDetails mScenicOrderDetails;
    private String strNum,realNum;
//    private Button btnRefund;
    private String touristName,touristCardNo,touristTelephone;
    private String mPftOrderNum;
    private ArrayList<Contacter> mContacterArrayList=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refund_ticket);
        getTitleTextView().setText("订单详情");
        initView();
        getOrderDetails();

    }

    private void initView() {
        Intent in=getIntent();
        ticketId=in.getStringExtra("orderId");
        status=in.getStringExtra("status");
        mPftOrderNum=in.getStringExtra("pftId");
        mStatusTextView=(TextView)findViewById(R.id.textView_status);
        mTicketNameTextView=(TextView)findViewById(R.id.ticket_name);
        mTicketAmountTextView=(TextView)findViewById(R.id.ticket_amount_price);
        mUseSituationTextView=(TextView)findViewById(R.id.ticket_use_situation);
        mOrderNoteImageView=(ImageView) findViewById(R.id.order_note);
        mTicketIdTextView=(TextView)findViewById(R.id.ticket_number);
        mCreateTimeTextView=(TextView)findViewById(R.id.order_time);
//        btnRefund=(Button)findViewById(R.id.button_refund);
//        btnRefund.setOnClickListener(this);
        mRecyclerView=(RecyclerView)findViewById(R.id.tourist_list);
        mTouristInfoAdapter=new TouristInfoAdapter();
        mRecyclerView.setAdapter(mTouristInfoAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

    }
    private void getOrderDetails() {
        showHud();
        Retrofit retrofit=new HttpHelper.HttpServiceBuilder().build().getRetrofit();
        HttpService service=retrofit.create(HttpService.class);
        Call<ListResultV<ScenicOrderDetails>> call=service.getScenicOrderDetails(ticketId);
        call.enqueue(new Callback<ListResultV<ScenicOrderDetails>>() {
            @Override
            public void onResponse(Call<ListResultV<ScenicOrderDetails>> call, Response<ListResultV<ScenicOrderDetails>> response) {
                hideHud();
                ListResultV listResultV=response.body();
                if(listResultV.getHead().getSuccess()) {
                    Gson gson = new Gson();
                    Map<String, String> scenicMap = listResultV.getBody().getData();
                    Type type = new TypeToken<ScenicOrderDetails>() {
                    }.getType();
                    String strScenicOrderJSON = new Gson().toJson(scenicMap);
                    mScenicOrderDetails = gson.fromJson(strScenicOrderJSON, type);
                    if("0".equals(mScenicOrderDetails.getStatus())){
                        mStatusTextView.setText("待付款");
                    }else if("1".equals(mScenicOrderDetails.getStatus())){
                        mStatusTextView.setText("待使用");
                    }else if("2".equals(mScenicOrderDetails.getStatus())){
                        mStatusTextView.setText("已取消");
                    }else if("3".equals(mScenicOrderDetails.getStatus())){
                        mStatusTextView.setText("已使用");
                    }else{
                        mStatusTextView.setText("已退款");
                    }
                    strNum=mScenicOrderDetails.getNum()+"";
                    realNum=mScenicOrderDetails.getRealNum()+"";

                    mTicketNameTextView.setText(mScenicOrderDetails.getPltitle()+"("+mScenicOrderDetails.getTtitle()+")"+mScenicOrderDetails.getNum()+"张");
                    mTicketAmountTextView.setText(mScenicOrderDetails.getTotalPrice()+"元");
                    mUseSituationTextView.setText("共"+mScenicOrderDetails.getNum()+"张,"+"已使用"+mScenicOrderDetails.getRealNum()+"张,"+"已退"+mScenicOrderDetails.getReturnNum()+"张");
                    mTicketIdTextView.setText(mScenicOrderDetails.getOrderId());
                    mCreateTimeTextView.setText(mScenicOrderDetails.getCreateTime());
                    strNum=mScenicOrderDetails.getNum()+"";
                    realNum=mScenicOrderDetails.getRealNum() + "";
                    touristName=mScenicOrderDetails.getOrdername();
                    touristCardNo=mScenicOrderDetails.getPersonID();
                    touristTelephone=mScenicOrderDetails.getOrdertel();
                    String[] names = touristName.split(",");
                    String[] phones = touristTelephone.split(",");
                    String[] cardNos = touristCardNo.split(",");

                    for(int i=0; i<names.length; i++)
                    {
                        String strName = names[i];
                        String strPhone = phones[i];
                        String strCardNo = cardNos[i];
                        Contacter contacter = new Contacter();

                        contacter.setName(strName);
                        contacter.setTelephone(strPhone);
                        contacter.setCardNo(strCardNo);
                        mContacterArrayList.add(contacter);
                    }
                    mTouristInfoAdapter.notifyDataSetChanged();


                }
            }

            @Override
            public void onFailure(Call<ListResultV<ScenicOrderDetails>> call, Throwable t) {
                hideHud();
                Toast.makeText(RefundTicketActivity.this,t.getMessage(),Toast.LENGTH_SHORT).show();

            }
        });

    }


    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.order_note:
                Intent in=new Intent(RefundTicketActivity.this,ScheduleNoteActivity.class);
                startActivity(in);
                break;
//            case R.id.btn_refund:
//                refund();
//                break;
        }

    }

    private void refund() {
        showHud();
        Retrofit retrofit=new HttpHelper.HttpServiceBuilder().build().getRetrofit();
        HttpService service=retrofit.create(HttpService.class);
        Call<Result> call=service.refund(mPftOrderNum,ticketId,realNum);
        call.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                hideHud();
                Gson gson=new Gson();
                Result result=response.body();
                Boolean isSuccess=result.getHead().getSuccess();
                if(isSuccess){
                    Toast.makeText(RefundTicketActivity.this,"退票成功",Toast.LENGTH_SHORT).show();
                    finish();
                }else{
                    Toast.makeText(RefundTicketActivity.this,"退票失败",Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {
                hideHud();
                Toast.makeText(RefundTicketActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();

            }
        });

    }

    private class TouristInfoAdapter extends RecyclerView.Adapter<TouristInfoViewHolder>{
        @Override
        public TouristInfoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater = LayoutInflater.from(RefundTicketActivity.this);
            View view = layoutInflater.inflate(R.layout.tourist_info_recycle_item, parent, false);
            return new TouristInfoViewHolder(view);
        }

        @Override
        public void onBindViewHolder(TouristInfoViewHolder holder, int position) {
            Contacter contacter=mContacterArrayList.get(position);
            holder.bindData(contacter);

        }

        @Override
        public int getItemCount() {
            return mContacterArrayList.size();
        }
    }

    private class TouristInfoViewHolder extends RecyclerView.ViewHolder{
        private TextView mTouristNameTextView;
        private TextView mTouristIdcardNoTextView;
        private TextView mtouristTelephoneTextView;
        public TouristInfoViewHolder(View itemView) {
            super(itemView);
            mTouristNameTextView=(TextView)itemView.findViewById(R.id.tourist_name);
            mTouristIdcardNoTextView=(TextView)itemView.findViewById(R.id.idcard_numbers);
            mtouristTelephoneTextView=(TextView)itemView.findViewById(R.id.telephone_number);
        }


        public void bindData(Contacter contacter) {
            mTouristNameTextView.setText(contacter.getName());
            mTouristIdcardNoTextView.setText(contacter.getCardNo());
            mtouristTelephoneTextView.setText(contacter.getTelephone());

        }
    }
}
